package cc.fs.kotlin.utils;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

/**
 * Created by fostion on 2/18/16.
 * RecycleView 上拉刷新/下拉加载更多 帮助类
 */
public class SwipeHelper {
    private Boolean isLoading = false;
    private Boolean isCanUpdate = true;
    private Boolean isCanLoadMore = true;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private SwipeListener swipeListener;

    public SwipeHelper(SwipeRefreshLayout _swipeRefreshLayout, RecyclerView _recycleView, SwipeListener _swipeListener) {
        this.swipeRefreshLayout = _swipeRefreshLayout;
        this.recyclerView = _recycleView;
        this.swipeListener = _swipeListener;

        setUp();
    }

    public void setUp() {
        /** 监听刷新请求 */
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (swipeListener != null && !isLoading) {
                    isLoading = true;
                    swipeRefreshLayout.setEnabled(false);
                    swipeListener.onRefresh();
                }
            }
        });

        /** 监听家家加载更多请求 */
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                //没有数据的时候不触发
                if (recyclerView.getAdapter() == null || recyclerView.getAdapter().getItemCount() == 0) {
                    return;
                }

                //停止滑动
                if (newState == RecyclerView.SCROLL_STATE_IDLE && swipeListener != null && isCanLoadMore && !isLoading) {
                    checkLoadmore();
                }
            }
        });
    }

    /** 取消上拉刷新 */
    public void setIsCanUpdate(boolean isCanUpdate) {
        this.isCanUpdate = isCanUpdate;
        if (isCanUpdate) {
            swipeRefreshLayout.setEnabled(true);
            swipeRefreshLayout.setRefreshing(false);
        } else {
            swipeRefreshLayout.setEnabled(false);
            swipeRefreshLayout.setRefreshing(false);
        }
    }

    /** 取消下拉加载更多 */
    public void setIsCanLoadMore(boolean isCanLoadMore) {
        this.isCanLoadMore = isCanLoadMore;
    }

    /** 结束任务 */
    public void finishThisLoading() {
        isLoading = false;
        swipeRefreshLayout.setEnabled(true);
        swipeRefreshLayout.setRefreshing(false);
    }

    public interface SwipeListener {
        /** 刷新 */
        void onRefresh();

        /** 加载更多 */
        void onLoadMore();
    }

    /** 检查是否需要执行Loadmore */
    private void checkLoadmore() {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            int lastVisibleItem = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();

            int totalItemCount = recyclerView.getAdapter().getItemCount();
            if (lastVisibleItem >= totalItemCount - 1) {
                isLoading = true;
                swipeRefreshLayout.setEnabled(false);
                swipeListener.onLoadMore();
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager gridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            int last[] = new int[gridLayoutManager.getSpanCount()];
            gridLayoutManager.findLastVisibleItemPositions(last);

            int totalItemCount = recyclerView.getAdapter().getItemCount();
            for (int i = 0; i < last.length; i++) {
                int lastVisibleItem = last[i];
                if (lastVisibleItem >= totalItemCount - 1) {
                    isLoading = true;
                    swipeRefreshLayout.setEnabled(false);
                    swipeListener.onLoadMore();
                    break;
                }
            }
        }
    }
}
